import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:rainflutter/Tools/dcp_pch.dart';

class NsuserDefault extends StatefulWidget {
  @override
  _NsuserDefaultState createState() => _NsuserDefaultState();
}

class _NsuserDefaultState extends State<NsuserDefault> {
  int clickNum = 0;
  int clickNumRead = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        children: <Widget>[
          SizedBox(
            height: 100,
          ),
          Container(
            child: FlatButton(
              color: Colors.blue,
              padding: EdgeInsets.all(0),
              child: Text(
                '点击写入',
                style: TextStyle(color: Colors.red, fontSize: 20),
              ),
              onPressed: () {
                saveInfo('name' + clickNum.toString(),
                    '我写了' + clickNum.toString() + '次了');
                clickNum++;
              },
            ),
          ),
          Container(
            child: FlatButton(
              color: Colors.blue,
              padding: EdgeInsets.all(0),
              child: Text(
                '点击读取',
                style: TextStyle(color: Colors.red, fontSize: 20),
              ),
              onPressed: () {
                // saveInfo('name'+ clickNum.toString(), '我写了'+clickNum.toString()+'次了');
                readInfo('name' + clickNumRead.toString());
                clickNumRead++;
              },
            ),
          ),
          Container(
            child: Stack(
              fit: StackFit.passthrough,
              children: <Widget>[
                Container(
                  width: 300,
                  height: 120,
                  color: Colors.red,
                  child: new Swiper(
                    itemBuilder: (BuildContext context, int index) {
                      // return new Image.network(
                      //   "http://via.placeholder.com/350x150",
                      //   fit: BoxFit.fill,
                      // );
                      return cellWidget();
                    },
                    itemCount: 10,
                    loop: true, //无限滚动
                    autoplay: true, //自动滚动
                    scrollDirection:
                        Axis.vertical, //Axis.horizontal左右 Axis.vertical上下

                    pagination: null, //new SwiperPagination()
                    control: null, //new SwiperControl()
                  ),
                ),Positioned(
                  top: 110,
                      left: 0,
                      right: 0,
                      child: Text('sdsdhfjkhskdjhf'),
                )
              ],
            ),
          )
        ],
      ),
    );
  
  
  }

  Widget cellWidget() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          child: Text('158****3231'),
        ),
        Container(
          child: Row(
            children: <Widget>[
              new Image.network(
                "http://via.placeholder.com/50x15",
                fit: BoxFit.fill,
              ),
              Text('正在联系'),
            ],
          ),
        ),
        Container(
          child: Text('134****7890'),
        ),
        Container(
          child: Text('133****3333'),
        ),
      ],
    );
  }

  void saveInfo(String name, String info) async {
    try {
      // print('写入了$name:$info');
      showToast('写入了$name:$info');
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setString(name, info);
    } catch (e) {
      print(e);
    }
  }

  void readInfo(String name) async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      // print('读取了$name:'+prefs.getString(name));
      showToast('读取了$name:' + prefs.getString(name));
    } catch (e) {
      print(e);
    }
  }
}
